var
    gulp = require('gulp'),                             //本地安装gulp所用到的地方
    less = require('gulp-less'),                        //less编译
    autoprefixer = require('gulp-autoprefixer'),        //自动前缀
    minify = require('gulp-clean-css'),                //css压缩
    uglify = require('gulp-uglify'),                    //js压缩
    concat = require('gulp-concat'),                    //js合并
    notify = require('gulp-notify'),                    //编译提示
    watch = require('gulp-watch'),                      //变动监听
    rename = require('gulp-rename'),                    //更改名称
    append = require('gulp-rev-append');                //增加版本号
//++++++++
sourcemaps = require('gulp-sourcemaps');            //
plumber = require('gulp-plumber');                  //避免因错误而中断 gulp
revReplace = require('gulp-rev-replace');           //替换引用路径
runSequence  = require('run-sequence');             //独立任务
//del = require('del');                               //删除文件
useref = require('gulp-useref');                    //html引用文件合并

//路径
var path = {
    less:'src/less/',
    css:'src/css/',
    com:'component/',

    //打包的路径
    out:'dist/',
    concatCss:[
        'component/jquery/layout/**/*.css',

        'component/jquery/form/**/*.css',
        'component/jquery/Navigation/**/*.css',
        'component/jquery/notice/**/*.css',

        "!component/jquery/Navigation/tab/**/*"
    ],
    concatJs:[
        'component/jquery/form/**/*.js',
        'component/jquery/Navigation/**/*.js',
        'component/jquery/notice/**/*.js',

        "!component/jquery/Navigation/tab/**/*"
    ]
};

//监听单个样式变动
gulp.task('data-watch', function() {
    gulp.watch(path.less + '**/*.less').on('all',function(status,event){
        var str = event;
        str = str.match(/([^\\]+)\.less$/)[1];
        var watchName = str.replace(/\\/,'/');
        return  gulp.src(path.less + '**/' + watchName +'.less')//当前路径{base:'./'}
            .pipe(less())
            .pipe(autoprefixer())
            //.pipe(cssmin())
            .pipe(gulp.dest(path.css))
            .pipe(notify({ message: 'Css编译完成' }));
        //gulp.start('data-style');
    });
});



//编译文件夹所有样式
gulp.task('data-all', function () {
    return  gulp.src(path.less + "**/*.less")
        .pipe(less())
        .pipe(autoprefixer())
        .pipe(minify({}))
        .pipe(gulp.dest(path.css))
        .pipe(notify({ message: 'Css编译完成' }));
});




//com******************************************

gulp.task('com-watch', function() {
    gulp.watch(path.com + '**/*.less').on('all',function(status,event){
        var str = event;
        str = str.match(/\\([a-zA-Z-_0-9]+)\.less$/)[1];
        var watchName = str.replace(/\\/,'/');
        return  gulp.src(path.com + '**/' + watchName +'.less')
            .pipe(less())
            .pipe(autoprefixer())
            //.pipe(cssmin())
            .pipe(gulp.dest(path.com))
            .pipe(notify({ message: 'Css编译完成' }));
        //gulp.start('data-style');
    });
});
//原生组件
gulp.task('com-all', function () {
    return  gulp.src(path.com + '**/*.less')
        .pipe(less())
        .pipe(autoprefixer())
        //.pipe(minify({}))
        .pipe(gulp.dest(path.com))
        .pipe(notify({ message: 'Css编译完成' }));
});


/**
 **  打包文件
 **/

//合并css文件并移动至目标文件夹
gulp.task('concatCss',function(){
    return gulp.src(path.concatCss)
        .pipe(concat('com-component.min.css'))
        .pipe(minify())
        .pipe(gulp.dest(path.out));
});
//合并js文件移动至目标文件夹
gulp.task('concatJs',function(){
    return gulp.src(path.concatJs)
        .pipe(concat('com-component.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest(path.out));
});

gulp.task('com-build',gulp.series('concatCss','concatJs'));